package edu.gatech.asthma.activity;

import java.util.List;

import edu.gatech.asthma.R;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.database.ProfileVO;
import edu.gatech.asthma.ui_support.HelpMenuHelper;
import edu.gatech.asthma.ui_support.ProfileCustomizer;
import edu.gatech.asthma.ui_support.login.ViewPagerAdapter;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Child interface for profile creation
 * Allows user to customize color and image of profile
 */
public class EditProfileChildActivity extends FragmentActivity {
	ViewPagerAdapter viewAdapter;
	ViewPager viewPager;
	ProfileCustomizer customizer;
	int identifier = -1;
	DBAdapter database;
	Activity currentActivity = this;	//necessary for dialog listeners
	ProfileVO p;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customize_profile);
        
        database = new DBAdapter(this);
        database.open();
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	identifier = extras.getInt("ID");
        }
        
        p = database.getProfile(identifier);
        customizer = new ProfileCustomizer(this,this.findViewById(android.R.id.content),database,identifier);
        customizer.setColors(p.getBgColor(),p.getTintColor(),p.getAccentColor());
        customizer.setCustomizations();

        List<Fragment> fragments = customizer.getCharacterImgs();

        viewAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager = (ViewPager) findViewById(R.id.profile_pic_gallery);
        viewPager.setAdapter(viewAdapter);
        viewPager.setCurrentItem(p.getPic());
        
        //set proper image tints
        //ImageView imgTint = (ImageView) findViewById(R.id.profile_img_tint);
        //imgTint.setColorFilter(getResources().getColor(ResourceHelper.getColor("character_color"+p.getTintColor())));
		//ImageView imgAccent = (ImageView) findViewById(R.id.profile_img_accent);
		//imgAccent.setColorFilter(getResources().getColor(ResourceHelper.getColor("character_color"+p.getAccentColor())));
        
        final EditText nameField = (EditText) findViewById(R.id.profile_name);
        nameField.setText(p.getName());
        
        final TextView title = (TextView) findViewById(R.id.profile_child_title);
        title.setText(getString(R.string.profile_child_edit));
        
        //create a Dialog popup window to display profile background color options
    	final Dialog popupBg = new Dialog(EditProfileChildActivity.this);
    	popupBg.setContentView(R.layout.profile_color);
    	popupBg.setTitle(getString(R.string.profile_bg_color_select));
    	customizer.setColorListeners(popupBg, "profile_color");
    	//create a Dialog popup window to display profile character color options
    	final Dialog popupTint = new Dialog(EditProfileChildActivity.this);
    	popupTint.setContentView(R.layout.profile_color);
    	popupTint.setTitle(getString(R.string.profile_tint_color_select));
    	customizer.setColorListeners(popupTint, "character_color");
    	//create a Dialog popup window to display profile character accent color options
    	final Dialog popupAccent = new Dialog(EditProfileChildActivity.this);
    	popupAccent.setContentView(R.layout.profile_color);
    	popupAccent.setTitle(getString(R.string.profile_accent_color_select));
    	customizer.setColorListeners(popupAccent,"accent_color");
        
        //BUTTON LISTENERS
        
        //background color selection menu brings up popout layout of color choices
        final Button bgColor = (Button) findViewById(R.id.button_profile_color);
        bgColor.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	popupBg.show();
            }
        });
        
      //character color selection menu brings up popout layout of color choices
        final Button tintColor = (Button) findViewById(R.id.button_character_color1);
        tintColor.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	popupTint.show();
            }
        });
        
      //character accent color selection menu brings up popout layout of color choices
        final Button accentColor = (Button) findViewById(R.id.button_character_color2);
        accentColor.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	popupAccent.show();
            }
        });
        
        final Button next = (Button) findViewById(R.id.button_child_profile_next);
        next.setText(R.string.profile_done);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            		if (!nameField.getText().toString().equals("")) {
            			//update profile information
            			database.updateProfile(identifier, nameField.getText().toString(), p.getGender(), p.getBirthday(), p.getWeight(),
            					p.getWeight_unit(), p.getHeight(), p.getHeight_unit(), p.getEthnicity(),
                    			customizer.getBgColor(),customizer.getTintColor(),customizer.getAccentColor(),viewPager.getCurrentItem(),p.getBest());
            			Intent i = new Intent(EditProfileChildActivity.this, HomeScreenActivity.class);
                    	i.putExtra("ID", identifier);
                    	EditProfileChildActivity.this.startActivity(i);
            		}
            		else { //Child did not enter a name
            			Toast toast = Toast.makeText(currentActivity, 
            					currentActivity.getResources().getString(R.string.profile_name_blank), Toast.LENGTH_LONG);
            			toast.show();
            		}
            	}
        });
        
        final ImageButton left = (ImageButton) findViewById(R.id.profile_select_left);
        viewAdapter.setListenerLeft(left,viewPager,customizer);
        
        final ImageButton right = (ImageButton) findViewById(R.id.profile_select_right);
        viewAdapter.setListenerRight(right,viewPager,customizer);
    }
	
	/**
	 * User clicked the help button on this screen
	 * @param v ImageView clicked
	 */
	public void helpButtonClicked(View v) {
		HelpMenuHelper.displayHelpMessage(this);
	}

}
